<?php
namespace Admin\Controller;

class AdverController extends LoginController {
	public function __construct() {
		parent::__construct ();
		layout ( false );
	}
	public function index() {
		
		$this->db = D ( 'adver_data' );
		
		if(!empty($_POST)){
			$arr = $_POST;
			$this->checkToken($arr);
			$arr['up_date'] = date("Y-m-d H:i:s");
			if(empty($arr['id'])){
				$arr['creat_date'] = $arr['up_date'];
				$this->db->add($arr);
			}else{
				$this->db->save($arr);
			}
		}
		
		$count = $this->db->count (); // 查询满足要求的总记录数 $map表示查询条件
		$Page = $this->getPage ( $count );
		$show = $Page->show ();
		$sql = "select a.*,b.title as a_title 
				FROM think_adver_data as a left join think_adver  as b
				on a.a_id = b.id order by a.creat_date desc limit {$Page->firstRow},{$Page->listRows}";
		$list = $this->db->query($sql);
		$this->assign ( 'list', $list );
		$this->assign ( 'show', $show );
		$this->display ();
	}
	
	/**
	 * 增加广告分类
	 */
	public function addType(){
		$this->display('addType');
		
	}
	
	/**
	 * 修改分类
	 */
	public function upType(){
		$this->db = D('adver');
		$data = $this->db->find($_GET['id']);
		$this->assign(array(
				'data'=>$data,
		));
		$this->display("upType");
	}
	

	/**
	 * 修改分类
	 */
	public function upImg(){
		$this->db = D('adver_data');
		$data = $this->db->find($_GET['id']);
		$this->db = D('adver');
		$list = $this->db->select();
		$this->assign(array(
				'list'=>$list,
				'data'=>$data,
		));
		$this->display("upImg");
	}
	
	/**
	 * 分类列表
	 */
	public function listsType(){
		$this->db = D('adver');
		if(!empty($_POST)){
			$this->checkToken($_POST);
			$arr = $_POST;
			$arr['up_date']=date("Y-m-d H:i:s");
			if(empty($arr['id'])){
				$arr['creat_date']=$arr['up_date'];
				$this->db->add($arr);
			}else{
				$this->db->save($arr);
			}
		}
		$list = $this->db->select();
		$this->assign(array(
				'list'=>$list,
		));
		$this->display("listsType");
	}
	
	/**
	 * 新增图片
	 */
	public function addImg(){
		$this->db = D('adver');
		$list = $this->db->select();
		$this->assign(array(
				'list'=>$list,
		));
		$this->display("addImg");
	}
	
	/**
	 * 删除图片
	 */
	public function imgDe() {
		$this->db = D ( 'adver_data' );
		if (IS_POST) {
			echo $result = $this->db->delete ( $_POST ['id'] );
		}
	}
	
	/**
	 * 删除类别
	 */
	public function typeDe(){
		$this->db = D ( 'adver' );
		if (IS_POST) {
			echo $result = $this->db->delete ( $_POST ['id'] );
		}
	}
}